package com.yonyou.pmclouds.safetypatrol.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.ref.itf.RefCodeConst;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolDetailVO;
import com.yonyou.pmclouds.safetypatrol.entity.SafetypatrolVO;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolDetailMapper;
import com.yonyou.pmclouds.safetypatrol.mapper.SafetypatrolMapper;
import com.yonyou.pmclouds.safetypatrol.process.SafeDetailAddCheckProcess;
import com.yonyou.pmclouds.safetypatrol.process.SafetypatrolInsertCheckProcess;
import com.yonyou.pmclouds.safetypatrol.process.SafetypatrolUpdateCheckProcess;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolDetailQuery;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolQuery;
import com.yonyou.pmclouds.safetypatrol.service.rmiitf.SafetypatrolService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

//import com.yonyou.pmclouds.safetypatrol.process.SafetypatrolDeleteCheckProcess;


@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = SafetypatrolService.class)
public class SafetypatrolServiceImpl implements SafetypatrolService {

    @Resource
    private SafetypatrolMapper safetypatrolMapper;
    @Resource
    private SafetypatrolDetailMapper safetypatrolDetailMapper;
    @Resource
    private SafetypatrolQuery safetypatrolQuery;
    @Resource
    private PictureRemoteService pictureRemoteService;
    @Resource
    private SafetypatrolDetailQuery detailQuery;

    @Resource
    private ReferenceChecker referenceChecker;



    //首页创建按钮
    @Override
    public SafetypatrolVO insert(SafetypatrolVO safetypatrolVO) throws BusinessException {
        //插入是对主从表同时插入 所以需要两个模板
        SingleInsertBpTemplate<SafetypatrolVO> singleInsertBpTemplate = new SingleInsertBpTemplate(safetypatrolMapper);

        MultiInsertBpTemplate<SafetypatrolDetailVO> detailVOMultiInsertBpTemplate = new MultiInsertBpTemplate<>(safetypatrolDetailMapper);
        //插入操作 首先检查主表中有没有相同的检查标准
        singleInsertBpTemplate.addBeforeProcessor(new SafetypatrolInsertCheckProcess(safetypatrolMapper));

        //插入操作 检查子表中有没有相同的检查项
        detailVOMultiInsertBpTemplate.addBeforeProcessor(new SafeDetailAddCheckProcess());
        //补全参数
        singleInsertBpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<SafetypatrolVO>());

        detailVOMultiInsertBpTemplate.addBeforeProcessor(new AuditInfoInsertMultiProcessor<SafetypatrolDetailVO>());
        //执行插入
        singleInsertBpTemplate.insert(safetypatrolVO);
        //设置主从表关联关系 其实就是给每一个细节类的外键赋值 给细节类赋值
        if (safetypatrolVO.getSafeDetailVOS()!=null && safetypatrolVO.getSafeDetailVOS().length>0){
        for (SafetypatrolDetailVO safetypatrolDetailVO  : safetypatrolVO.getSafeDetailVOS()) {
            safetypatrolDetailVO.setPkSafetypatrol(safetypatrolVO.getPkSafetypatrol());
        }
        //子表批量插入执行 使用批量插入之后就不能根据创建时间进行排序了
            SafetypatrolDetailVO[] safeDetailVOS = safetypatrolVO.getSafeDetailVOS();
            detailVOMultiInsertBpTemplate.insert(safeDetailVOS);
        //添加事例照片  个数限制 编号设置
        insertExamplePics(safetypatrolVO);
        }

        return safetypatrolVO;
    }


    //通用按钮 插入示例图片
    @Override
    public SafetypatrolVO insertExamplePics(SafetypatrolVO safetypatrolVO) throws BusinessException {
        if (safetypatrolVO==null &&safetypatrolVO.getSafeDetailVOS()==null){
            return safetypatrolVO;
        }

        List<PictureVO> pictureVOList = setSimplePicture(safetypatrolVO);
        if (pictureVOList!=null &&pictureVOList.size()>0){
           pictureRemoteService.insertPictures(pictureVOList.toArray(new PictureVO[pictureVOList.size()]));
        }

        return safetypatrolVO;
    }

    //通用按钮 删除
    @Override
    public void delete(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleDeleteBpTemplate<SafetypatrolVO> safetypatrolVOSingleDeleteBpTemplate =new SingleDeleteBpTemplate<>(safetypatrolMapper);
        //这边说会自动校验是否被引用
        safetypatrolVOSingleDeleteBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<SafetypatrolVO>());
        safetypatrolVOSingleDeleteBpTemplate.delete(safetypatrolVO);
        //因为示例图片属于公共接口 这里再删除
        pictureRemoteService.deleteExamplePicsOfBill(safetypatrolVO.getPkSafetypatrol());
    }

    //首页编辑按钮
    @Override
    public SafetypatrolVO update(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleUpdateBpTemplate<SafetypatrolVO> safetypatrolVOSingleUpdateBpTemplate =new SingleUpdateBpTemplate<SafetypatrolVO>(safetypatrolMapper, new String[]{"standardName","standardType"});
        //细节类操作模板
        SingleUpdateBpTemplate<SafetypatrolDetailVO> detailVOSingleUpdateBpTemplate =new SingleUpdateBpTemplate<SafetypatrolDetailVO>(safetypatrolDetailMapper, new String[]{"checkname","checkdescription","needphoto","ordercode"});
        SingleInsertBpTemplate<SafetypatrolDetailVO> detailVOSingleInsertBpTemplate = new SingleInsertBpTemplate<>(safetypatrolDetailMapper);
        SingleDeleteBpTemplate<SafetypatrolDetailVO> detailVOSingleDeleteBpTemplate = new SingleDeleteBpTemplate<>(safetypatrolDetailMapper);
        //批量模板
        MultiInsertBpTemplate<SafetypatrolDetailVO> detailVOMultiInsertBpTemplate = new MultiInsertBpTemplate<>(safetypatrolDetailMapper);

        //主表添加公有字段
        safetypatrolVOSingleUpdateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<SafetypatrolVO>());
        
        //主表检验是否重命名
        safetypatrolVOSingleUpdateBpTemplate.addBeforeProcessor(new SafetypatrolUpdateCheckProcess(safetypatrolMapper) );

        //主表执行修改
        safetypatrolVOSingleUpdateBpTemplate.update(safetypatrolVO);

        //从表更新前添加公有字段
        detailVOSingleUpdateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<SafetypatrolDetailVO>());
        //从表插入前添加公有字段
        detailVOMultiInsertBpTemplate.addBeforeProcessor(new AuditInfoInsertMultiProcessor<>());
        //从表删除前添加公有字段
        detailVOSingleDeleteBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<SafetypatrolDetailVO>());

        List<SafetypatrolDetailVO> list =new ArrayList<>();
        //执行子表修改操作
        SafetypatrolDetailVO[] safeDetailVOS = safetypatrolVO.getSafeDetailVOS();

        if (safeDetailVOS!=null && safeDetailVOS.length>0){

            //传过来的数据 要么有要么没有 没有就新增 有就更改
            for (SafetypatrolDetailVO safeDetailVO : safeDetailVOS) {

                if (safeDetailVO.getPkSafetypatroldetail()!=null && safeDetailVO.getPkSafetypatroldetail()!=""){
                    detailVOSingleUpdateBpTemplate.update(safeDetailVO);

                    //更新示例图片 直接使用公共接口即可
                    List<PictureVO> pictureVOList = setSimplePicture(safetypatrolVO);
                    //注意这里需要的是数组
                    pictureRemoteService.updateExamplePics(pictureVOList.toArray(new PictureVO[pictureVOList.size()]), safetypatrolVO.getPkSafetypatrol());
                }
                else if(safeDetailVO.getPkSafetypatroldetail()==null || safeDetailVO.getPkSafetypatroldetail()==""){
                    safeDetailVO.setPkSafetypatrol(safetypatrolVO.getPkSafetypatrol());
                    list.add(safeDetailVO);
                }

                //接下来判断传入的数据有没有删除的数据 有就进行删除 这里最好让前端传入子表主键值 直接赋值就好
                //得到之前的细节值
                List<SafetypatrolDetailVO> orgDetailVos = safetypatrolDetailMapper.findById(safeDetailVO.getPkSafetypatrol());
                //得到传入的细节值
                SafetypatrolDetailVO[] nowDetailVos = safetypatrolVO.getSafeDetailVOS();
                //得到需要删除的细节id集合
                List<String> deletePkDetailList = needDeletePkDetailList(orgDetailVos, nowDetailVos);

                //执行删除细节操作
                for (String s : deletePkDetailList) {
                   detailVOSingleDeleteBpTemplate.delete(safetypatrolDetailMapper.getById(s));
                }

            }

            if (CollectionUtils.isNotEmpty(list)){
                //批量添加
                SafetypatrolDetailVO[] safetypatrolDetailVOS = list.toArray(new SafetypatrolDetailVO[0]);
                safetypatrolVO.setSafeDetailVOS(safetypatrolDetailVOS);
                detailVOMultiInsertBpTemplate.insert(safetypatrolDetailVOS);
                //插入示例图片
                insertExamplePics(safetypatrolVO);

            }

        }

        //返回更新后的实体对象
        return safetypatrolVO;
    }





    //首页的启用按钮
    @Override
    public SafetypatrolVO enable(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleUpdateBpTemplate<SafetypatrolVO> updateBpTemplate =new SingleUpdateBpTemplate(safetypatrolMapper,new String[]{"statecode"} );
        if(safetypatrolVO!=null ){
            updateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
            safetypatrolVO.setStatecode(EnableStatusConst.ENABLE);
            SafetypatrolVO update = updateBpTemplate.update(safetypatrolVO);

            return update;
        }
      return null;
    }

    //首页的停用按钮
    @Override
    public SafetypatrolVO disable(SafetypatrolVO safetypatrolVO) throws BusinessException {
        SingleUpdateBpTemplate<SafetypatrolVO> updateBpTemplate =new SingleUpdateBpTemplate(safetypatrolMapper,new String[]{"statecode"} );
        if(safetypatrolVO!=null ){
            updateBpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<>());
            safetypatrolVO.setStatecode(EnableStatusConst.DISABLE);
            //通过这个方法返回 不是直接返回
            return updateBpTemplate.update(safetypatrolVO);

        }
        return null;
    }

    //通用方法 查看数据是否被引用
    @Override
    public boolean referCheck(String pk) throws BusinessException {

        return referenceChecker.checkReferenced(RefCodeConst.SAFETYPATROL,new String[]{pk} );

    }

    //service层方法 获得需要删除的主键
    private List<String> needDeletePkDetailList(List<SafetypatrolDetailVO> orgDetailVos, SafetypatrolDetailVO[] nowDetailVos) {
        List<String> needList =new ArrayList<>();
        if (orgDetailVos ==null && orgDetailVos.size()==0){
            //对之前数据进行非空校验 若为空则有问题 无法进行删除操作
            return needList;
        }
        //把传过来的主键放到list集合中作为比较条件
        List<String> compareList=new ArrayList<>();
        for (SafetypatrolDetailVO nowDetailCompare : nowDetailVos) {
            compareList.add(nowDetailCompare.getPkSafetypatroldetail());
        }

        for (SafetypatrolDetailVO orgDetailCompare : orgDetailVos) {

            System.out.println(orgDetailCompare.getPkSafetypatroldetail());

            if (!compareList.contains(orgDetailCompare.getPkSafetypatroldetail())){
                needList.add(orgDetailCompare.getPkSafetypatroldetail());
            }
        }

        return needList;
    }



    //service层方法 设置示例图片方法
    List<PictureVO> setSimplePicture(SafetypatrolVO safetypatrolVO) throws BusinessException {
        List<PictureVO> pictureVOList =new ArrayList<>();
        SafetypatrolDetailVO[] safeDetailVOS = safetypatrolVO.getSafeDetailVOS();

        for (SafetypatrolDetailVO safetypatrolDetailVO : safeDetailVOS) {
            if (ArrayUtils.isEmpty(safetypatrolDetailVO.getExamplePictures()) ){
                continue;
            }
            if (safetypatrolDetailVO.getExamplePictures().length>5){
                throw new BusinessException("示例图片数量不能超过5张");
            }
            int i =1;
            //给分离出的图片赋值编号 设置示例图片两个关联外键 设置顺序 设置exampleflag
            for (PictureVO pictureVO : safetypatrolDetailVO.getExamplePictures()) {
                pictureVO.setSequence(i);
                i++;
                pictureVO.setExampleFlag(1);
                pictureVO.setPk_entity(safetypatrolDetailVO.getPkSafetypatroldetail());
                pictureVO.setPk_bill(safetypatrolVO.getPkSafetypatrol());
                pictureVOList.add(pictureVO);
            }
        }

        return pictureVOList;
    }
}
